package com.tiantianyuegou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.tiantianyuegou.activity.BaseActivity;
import com.tiantianyuegou.R;
import com.tiantianyuegou.adapter.AddressAdapter;
import com.tiantianyuegou.adapter.SelectAddressAdapter;
import com.tiantianyuegou.constant.Constant;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.http.SPHelper;
import com.tiantianyuegou.model.AddressModel;
import com.tiantianyuegou.util.LoadingDialog;
import com.tiantianyuegou.util.Tools;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 收货地址
 * Created by Administrator on 2016/1/27.
 */
public class AddressActivity extends BaseActivity implements AdapterView.OnItemClickListener, View.OnClickListener {


    private TextView mTxt_title, mTxt_add;
    private ImageView mImg_back;
    private FrameLayout mFrame_noAddress, mFrame_haveAddress;
    private ListView mListView;
    private AddressAdapter adapter;
    private List<AddressModel> mData;

    private LoadingDialog mLoadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_address);
        Tools.changeSystemBar(this);
        initViews();
    }

    @Override
    public void findViewByIds() {
        mFrame_haveAddress = (FrameLayout) findViewById(R.id.select_address);
        mFrame_noAddress = (FrameLayout) findViewById(R.id.select_address_noAddress);
        mListView = (ListView) findViewById(R.id.select_address_list);
        mTxt_title = (TextView) findViewById(R.id.text_title);
        mTxt_add = (TextView) findViewById(R.id.text_menu);
        mTxt_add.setText("添加");
        mTxt_add.setVisibility(View.VISIBLE);
        mImg_back = (ImageView) findViewById(R.id.img_title);
        mTxt_title.setText("收货地址");

    }

    @Override
    public void initListener() {
        mImg_back.setOnClickListener(this);
        mTxt_add.setOnClickListener(this);
        mListView.setOnItemClickListener(this);
    }

    @Override
    public void initData() {
        mLoadingDialog = new LoadingDialog(this);
        mFrame_noAddress.setVisibility(View.VISIBLE);
        mFrame_haveAddress.setVisibility(View.GONE);
        mData = new ArrayList<>();
        adapter = new AddressAdapter(this);
        mListView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getAddressList();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent = new Intent();
        AddressModel model = new AddressModel();
        model = mData.get(position);
        Bundle bundle = new Bundle();
        bundle.putSerializable("models", model);
        intent.putExtras(bundle);
        intent.setClass(AddressActivity.this, UpdateAddressActivity.class);
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_title:
                finish();
                break;
            case R.id.text_menu:
                Intent intent = new Intent(AddressActivity.this, AddAddressActivity.class);
                startActivity(intent);
                break;
        }
    }

    private void getAddressList() {
        mLoadingDialog.setMsg("正在加载地址列表...");
        mLoadingDialog.show();

        RequestParams params = new RequestParams();
        params.addBodyParameter("tmId", SPHelper.getTmId());
        loadData(HttpRequest.HttpMethod.POST, ApiDefine.ADDRESS_LIST, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                mLoadingDialog.dismiss();
                Log.e("TAG", "getAddressList :" + responseInfo.result);
                if (TextUtils.isEmpty(responseInfo.result)) {
                    showMsg("获取地址列表失败");
                } else {
                    try {
                        JSONObject root = new JSONObject(responseInfo.result);
                        String code = root.getString("code");
                        if ("100".equals(code)) {
                            mData.clear();
                            mFrame_noAddress.setVisibility(View.GONE);
                            mFrame_haveAddress.setVisibility(View.VISIBLE);
                            JSONArray list = root.getJSONArray("list");
                            if (list.length() > 0){
                                mFrame_noAddress.setVisibility(View.GONE);
                                mFrame_haveAddress.setVisibility(View.VISIBLE);
                                for (int i = 0; i < list.length(); i++) {
                                    JSONObject value = list.getJSONObject(i);
                                    AddressModel model = new AddressModel();
                                    model.setId(value.getInt("tmaId"));
                                    model.setName(value.getString("tmaName"));
                                    model.setPhone(value.getString("tmaPhone"));
                                    model.setPostCode(value.getString("tmaPostCode"));
                                    model.setAddress(value.getString("tmaAddress"));
                                    model.setProvinceId(value.getInt("tmaProviceId") + "");
                                    model.setProvince(value.getString("provice"));
                                    model.setCityId(value.getInt("tmaCityId") + "");
                                    model.setCity(value.getString("city"));
                                    model.setCountId(value.getInt("tmaCountId") + "");
                                    model.setCounty(value.getString("county"));
                                    if (1 == value.getInt("tmaDefault")) {
                                        model.setIsDefault(true);
                                        model.setIsSelect(true);
                                    } else {
                                        model.setIsDefault(false);
                                        model.setIsSelect(false);
                                    }
                                    mData.add(model);
//                                    TODO 需换成真实数据字段名
                                    adapter.setData(mData);
                                    adapter.notifyDataSetChanged();
                                }
                            }else{
                                mFrame_noAddress.setVisibility(View.VISIBLE);
                                mFrame_haveAddress.setVisibility(View.GONE);
                            }
                        } else if ("103".equals(code)) {
                            mFrame_noAddress.setVisibility(View.VISIBLE);
                            mFrame_haveAddress.setVisibility(View.GONE);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Log.e("TAG", "request failure :" + s);
                showMsg("当前网络不可用，请检查您的网设置");
                mFrame_noAddress.setVisibility(View.VISIBLE);
                mFrame_haveAddress.setVisibility(View.GONE);
                mLoadingDialog.dismiss();
            }
        });
    }


}
